package com.zwxbc.funplay.service.impl;

import com.zwxbc.funplay.dao.GameDao;
import com.zwxbc.funplay.dao.impl.GameDaoImpl;
import com.zwxbc.funplay.entity.*;
import com.zwxbc.funplay.service.DeveloperService;
import com.zwxbc.funplay.service.GameCategoryService;
import com.zwxbc.funplay.service.GameService;
import com.zwxbc.funplay.service.PlayerGameLibraryService;

import java.util.List;
import java.util.Date;

public class GameServiceImpl implements GameService {
	GameDao gameDao = new GameDaoImpl();
	GameCategoryService gameCategoryService = new GameCategoryServiceImpl();
	DeveloperService developerService = new DeveloperServiceImpl();
	PlayerGameLibraryService playerGameLibraryService = new PlayerGameLibraryServiceImpl();
	PurchaseRecordServiceImpl purchaseRecordService = new PurchaseRecordServiceImpl();


	@Override
	public List<Game> getGameList() {
		List<Game> allGames = gameDao.getAllGames();
		for (Game game : allGames) {
			GameCategory gameCategory = gameCategoryService.getGameCategoryById(game.getCategoryId());
			if (gameCategory != null) {
				game.setCategoryName(gameCategory.getCategoryName());
			}
			Developer developer = developerService.getDeveloperById(game.getDeveloperId());
			if (developer != null) {
				game.setDeveloperName(developer.getDeveloperName());
			}
		}
		return allGames;
	}

	@Override
	public List<Game> getGameRatingTop() {
		List<Game> allGames = gameDao.getGameRatingTop();
		for (Game game : allGames) {
			GameCategory gameCategory = gameCategoryService.getGameCategoryById(game.getCategoryId());
			if (gameCategory != null) {
				game.setCategoryName(gameCategory.getCategoryName());
			}
			Developer developer = developerService.getDeveloperById(game.getDeveloperId());
			if (developer != null) {
				game.setDeveloperName(developer.getDeveloperName());
			}
		}
		return allGames;
	}

	@Override
	public boolean deleteGame(Long gameId) {
		int result = gameDao.deleteGame(gameId);
		return result > 0;
	}

	@Override
	public boolean updateGame(Game game) {
		int result = gameDao.updateGame(game);
		return result > 0;
	}

	@Override
	public boolean addGame(Game game) {
		int result = gameDao.addGame(game);
		return result > 0;
	}

	@Override
	public List<Game> getGameListByCategoryId(Long gameCategoryId) {
		List<Game> allGames = gameDao.getGameListByCategoryId(gameCategoryId);
		for (Game game : allGames) {
			GameCategory gameCategory = gameCategoryService.getGameCategoryById(game.getCategoryId());
			if (gameCategory != null) {
				game.setCategoryName(gameCategory.getCategoryName());
			}
			Developer developer = developerService.getDeveloperById(game.getDeveloperId());
			if (developer != null) {
				game.setDeveloperName(developer.getDeveloperName());
			}
		}
		return allGames;
	}

	@Override
	public List<Game> getGameListByNameLike(String gameName) {
		List<Game> allGames = gameDao.getGameListByNameLike(gameName);
		for (Game game : allGames) {
			GameCategory gameCategory = gameCategoryService.getGameCategoryById(game.getCategoryId());
			if (gameCategory != null) {
				game.setCategoryName(gameCategory.getCategoryName());
			}
			Developer developer = developerService.getDeveloperById(game.getDeveloperId());
			if (developer != null) {
				game.setDeveloperName(developer.getDeveloperName());
			}
		}
		return allGames;
	}

	@Override
	public void buyGame(Long gameId, Long playerId) {
		PlayerGameLibrary playerGameLibrary = new PlayerGameLibrary();
		playerGameLibrary.setGameId(gameId);
		playerGameLibrary.setPlayerId(playerId);
		playerGameLibrary.setPlaytime(0L);
		playerGameLibrary.setPurchaseDate(new Date());
		playerGameLibraryService.addPlayerGameLibrary(playerGameLibrary);
		Game game = gameDao.getGameById(gameId);
		PurchaseRecord purchaseRecord = new PurchaseRecord();
		purchaseRecord.setGameId(gameId);
		purchaseRecord.setPlayerId(playerId);
		purchaseRecord.setPurchasePrice(game.getPrice());
		purchaseRecord.setPurchaseDate(new Date());
		purchaseRecordService.addPurchaseRecord(purchaseRecord);
	}

	@Override
	public Game getGameById(Long gameId) {
		Game game = gameDao.getGameById(gameId);
		GameCategory gameCategory = gameCategoryService.getGameCategoryById(game.getCategoryId());
		if (gameCategory != null) {
			game.setCategoryName(gameCategory.getCategoryName());
		}
		Developer developer = developerService.getDeveloperById(game.getDeveloperId());
		if (developer != null) {
			game.setDeveloperName(developer.getDeveloperName());
		}
		return game;
	}
}
